package com.learn.criminalintent

import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.DialogFragment

private const val PHOTO_PATH = "PHOTO_PATH"

class PhotoViewFragment : DialogFragment() {
    private lateinit var photoView: ImageView
    private var photoPath: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        photoPath = arguments?.getSerializable(PHOTO_PATH) as String
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.dialog_photo_view, container, false)
        photoView = view.findViewById(R.id.zoom_image_view)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        photoPath?.let {
            photoView.setImageBitmap(getCorrectlyOrientedBitmap(it))
        }
    }

    companion object {
        fun newInstance(date: String): PhotoViewFragment {
            val args = Bundle().apply {
                putSerializable(PHOTO_PATH, date)
            }
            return PhotoViewFragment().apply {
                arguments = args
            }
        }
    }
}